import types, string, pprint, exceptions

class EnumException(exceptions.Exception):
	pass
	
class Enumeration:
	def __init__(self, name, enumList):
		self.__doc__ = name
		lookup = {}
		uniqueNames = []
		values = []
		for x in enumList:
			if type(x) == types.TupleType:
				x, i = x
			if type(x) != types.StringType:
				raise EnumException, "enum name is not a string: " + x
			if type(i) != types.IntType:
				raise EnumException, "enum value is not an integer: " + i
			if x in uniqueNames:
				raise EnumException, "enum name is not unique: " + x
			uniqueNames.append(x)
			values.append(i)
			lookup[x] = i
			i += 1
		self.lookup = lookup
		
	def __getattr__(self, attr):
		if not self.lookup.has_key(attr):
			raise AttributeError
		return self.lookup[attr]
		

class ReverseEnumeration:
	def __init__(self, name, enumList):
		self.__doc__ = name
		lookup = {}
		reverseLookup = {}
		i = 0
		uniqueNames = []
		uniqueValues = []
		for x in enumList:
			if type(x) == types.TupleType:
				x, i = x
			if type(x) != types.StringType:
				raise EnumException, "enum name is not a string: " + x
			if type(i) != types.IntType:
				raise EnumException, "enum value is not an integer: " + i
			if x in uniqueNames:
				raise EnumException, "enum name is not unique: " + x
			if i in uniqueValues:
				raise EnumException, "enum value is not unique: " + x
			uniqueNames.append(x)
			uniqueValues.append(i)
			lookup[x] = i
			reverseLookup[i] = x
			i += 1
		self.lookup = lookup
		self.reverseLookup = reverseLookup
		
	def __getattr__(self, attr):
		if not self.lookup.has_key(attr):
			raise AttributeError
		return self.lookup[attr]
		
	def whatIs(self, value):
		return self.reverseLookup[value]
	